<?php 
namespace XFCore\traits\user;
use lib\protocol\AliQQ_PayMoney;
use lib\Examine;
trait UPQrlistTrait{
    public function Get_Qr(){
        if($this->islogin_user != 1) {
            exitjson(-1, "请先登录");
        }
        $id = $this->input->id;
        if(empty($id))exitjson(-1,'非法操作！');
        $qr_row = $this->db->qrlistrow($id);
        if(empty($qr_row) || $qr_row['pid'] != $this->uid){
            exitjson(-1,'数据不存在！');
        }
        if($qr_row['hook_type'] != 0 && $qr_row['hook_type'] != 2){
            exitjson(-1,'该方式不支持更新');
        }
        if (in_array($qr_row['cookie'], ['xfqymckxy', 'xfqydmfxy'])){
            exitjson(-1,'当面付和免CK不需要更新哦亲！');
        }
        $data = [
            'id'=>$qr_row['id'],
            'type'=>$qr_row['type'],
            'beizhu'=>$qr_row['beizhu'],
            'xf_stat'=>$qr_row['xf_stat'],
            'hook_type'=>$qr_row['hook_type']
            ];
        if($qr_row['type'] == 'wxpay' && $qr_row['hook_type'] == 0 && !empty($qr_row['wx_name'])){
            $dy_row = $this->db->wechat_trumpetrow($qr_row['wx_name'] , 'wx_user' , "`wx_name` , `wx_user` , `status`");
            if($dy_row['status'] != 1 || empty($dy_row)){
                exitjson(-1,'店员数据不存在或未在线');
            }
            unset($dy_row['status']);
            $data['data'] = $dy_row;
        }
        exitjson(200,'获取成功！',$data);
    }
    public function Up_Qrcookie(){
        if($this->islogin_user != 1) {
            exitjson(-1, "请先登录");
        }
        $id = $this->input->id;
        $cookie = $this->input->cookie;
        if(empty($id) || empty($cookie))exitjson(-1,'非法操作！');
        $qr_row = $this->db->qrlistrow($id);
        if(empty($qr_row) || $qr_row['pid'] != $this->uid){
            exitjson(-1,'数据不存在');
        }
        
        $this->user_vip($qr_row['type']);
        //如果是支付宝或QQ则验证一下 cookie 是否可用
        $money = 0.00;
        if($qr_row['type'] == 'alipay' || $qr_row['type'] == 'qqpay'){
            //支付宝验证一下 uid是否存在
            $tmp_cookie = base64_decode($cookie);
            if($qr_row['type'] == 'alipay'){
                $userId = getSubstr($tmp_cookie, "CLUB_ALIPAY_COM=", ";");
                $rs = $this->db->select('qrlist','alipayUId',['pid'=>$this->uid,'cookie'=>'xfqymckxy']);
                if($rs['count'] > 0){
                    foreach ($rs['result'] as $res){
                        $alipayUId=$srow['alipayUId'];
                        if($userId == $alipayUId){
                            exitjson(-1,'添加失败，此账号已经存在 请换个账号在试或者删除此账号的免CK通道');
                        }
                    }
                }
            }
            $methodName = 'Get_pay_money';
            
            if (method_exists('lib\protocol\AliQQ_PayMoney', $methodName)){
                    $Pay_Money = AliQQ_PayMoney::$methodName($qr_row['type'], $cookie);
            }else {
                exitjson(-1, '找不到对应的支付协议');
            }
            
            if(!$Pay_Money['status']){
                $typeMapping = [
                    'alipay' => '支付宝',
                    'qqpay' => 'QQ',
                ];
                $type_name = $typeMapping[$qr_row['type']];
                exitjson(-1,"更新失败,请刷新页面重试,请换个! ".$type_name." 账号重试");
            }
            $money = $Pay_Money['money'];
        }
        $data = [
            'cookie'=>$cookie,
            'money'=>$money,
            'status'=>1,
            'addtime'=>$this->date
        ];
        if($this->db->update('qrlist',$data,['id'=>$id])){
            exitjson(200,'更新成功！');
        }
        exitjson(-1,'更新失败！');
    }
    public function Get_Login_QrCode(){
        if($this->islogin_user != 1) {
            exitjson(-1, "请先登录");
        }
        $id = $this->input->id;
        if(empty($id))exitjson(-1,'非法操作！');
        
        // exitjson(200,'success',['type'=>'alipay']);
        $qr_row = $this->db->qrlistrow($id);
        if(empty($qr_row) || $qr_row['pid'] != $this->uid){
            exitjson(-1,'数据不存在！');
        }
        $this->user_vip($qr_row['type']);
        //获取二维码
        $res = $this->Popup_QRcode($qr_row);
        exitjson(200,'success',$res);
    }
    //分发获取二维码
    protected function Popup_QRcode($qr_row){
        switch($qr_row['type']){
            case 'alipay':
                $res = $this->cookie->Get_QrLogin($qr_row['type']);
                break;
            case 'qqpay':
                if($qr_row['xf_stat'] == 1){
                    
                    if(empty($qr_row['beizhu'])){
                        exitjson(-1,'QQ号不存在');
                    }
                    if($qr_row['xf_type'] == 2){
                        $res = $this->QQPC->Get_QrLogin('qqpay',$qr_row['beizhu']);
                    }else{
                        $res = $this->QQweb->Get_QQPCQrLogin('qqpay',$qr_row['beizhu'],$qr_row['xf_type']);
                    }
                    
                }else{
                    $res = $this->cookie->Get_QrLogin($qr_row['type']);
                }
                break;
            case 'wxpay':
                if($qr_row['hook_type'] != 2)exitjson(-1,'该码子数据不是微信云端');
                $wx = Examine::Wx_protocol($qr_row);
                if(is_array($wx) && $wx['code'] == -1)exitjson(-1,'未知协议类型');
                $jixin = Wx_jixin($qr_row['xf_type']);
                $DeviceID = '';
                if($jixin == 'Ipad'){
                    $DeviceID = $qr_row['data_data']?:"";
                }
                $res = $wx->wxlogin($DeviceID);
                $res['type'] = 'wxpay';
                $res['jixin'] = $jixin;
                if($res['jixin'] == 'Ipad'){
                    if(empty($qr_row['data_data']))
                    {
                        $this->db->update('qrlist',['data_data'=>$res['id']],['id'=>$qr_row['id']]);
                    }
                }
                
                break;
        }
        return $res; 
    }
    
    public function Submit_Captcha(){
        if($this->islogin_user != 1) {
            exitjson(-1, "请先登录");
        }
        $id  = $this->input->id;
        $uuid = $this->input->uuid;
        $captcha = $this->input->captcha;
        if(empty($uuid) || empty($id))exitjson(-1,'非法操作！');
        if(empty($captcha))exitjson(-1,'请输入验证码');
        $row = $this->db->qrlistrow($id);
        if(empty($row) || $row['pid'] != $this->uid){
            exitjson(-1,'数据不存在！');
        }
        if(Wx_jixin($row['xf_type']) != 'Ipad'){
            exitjson(-1,'该协议不支持该操作！');
        }
        $wx = Examine::Wx_protocol($row);
        if(is_array($wx) && $wx['code'] == -1)exitjson(-1,'未知协议类型');
        $res = $wx->VerifyCode($uuid , $captcha);
        exitjson($res['code'],$res['msg']);
    }
    
    public function Get_Login_Cookie(){
        if($this->islogin_user != 1) {
            exitjson(-1, "请先登录");
        }
        $id = $this->input->id;
        $qr_id = $this->input->qr_id;
        $uuid  = $this->input->uuid ?:null;
        
        if(empty($id) || empty($qr_id))exitjson(-1,'非法操作！');
        $qr_row = $this->db->qrlistrow($id);
        if(empty($qr_row) || $qr_row['pid'] != $this->uid){
            exitjson(-1,'数据不存在！');
        }
        if($qr_row['type'] == 'wxpay' && $res['hook_type'] == 2 && empty($uuid)){
            exitjson(-1,'非法操作！');
        }
        $res = $this->Popup_QRcookie($qr_row , $qr_id , $uuid);
        if($res['code'] === 0)$res['ZT_ID'] = 1;
        exitjson($res['code'],'success',$res);
    }
    
    protected function Popup_QRcookie($qr_row , $qr_id , $uuid = null){
        switch($qr_row['type']){
            case 'alipay':
                $res = $this->cookie->Get_CooKie($qr_row['type'] , $qr_id);
                break;
            case 'qqpay':
                if($qr_row['xf_stat'] == 1){
                    if(empty($qr_row['beizhu'])){
                        exitjson(-1,'QQ号不存在');
                    }
                    if($qr_row['xf_type'] == 2){
                        $res = $this->QQPC->GET_JIANCE('qqpay',$qr_row['beizhu']);
                    }else{
                        $res = $this->QQweb->GET_JIANCE('qqpay',$qr_row['beizhu'],$qr_row['xf_type']);
                    }
                    
                }else{
                    $res = $this->cookie->Get_CooKie($qr_row['type'] , $qr_id);
                }
                break;
            case 'wxpay':
                $wx = Examine::Wx_protocol($qr_row);
                if(is_array($wx) && $wx['code'] == -1)exitjson(-1,'未知协议类型');
                $res = $wx->wxCheckLoginQrcode($qr_id , $uuid);
                
                break;
        }
        
        return $res;
    }
    
}
?>